package com.zhongzhi.serviceimpl;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.dao.AisleDao;
import com.zhongzhi.dao.EquipmentDao;
import com.zhongzhi.service.EquipmentService;
import com.zhongzhi.springboot.pojo.Aisle;
import com.zhongzhi.springboot.pojo.Bo;
import com.zhongzhi.springboot.pojo.Equipment;

@Service
public class EquipmentServiceImpl implements EquipmentService{

	@Autowired
	private EquipmentDao equipmentDao;
	@Autowired
	private AisleDao aisleDao;
	//增
	@Override
	public Bo insert(Equipment equipment) {
		//测试
		//		System.out.println(equipment);
		//		return new Bo(true,"输入ip地址格式不正确",0,equipment);
		//参数校验
		if(equipment.getHost()==null || equipment.getHost().length()==0) {
			return new Bo(false,"设备ip地址为空",0,null);
		}else if (equipment.getPlcName()==null || equipment.getPlcName().length()==0) {
			return new Bo(false,"设备名称为空",0,null);
		}else if (equipment.getPort()==null) {
			return new Bo(false,"设备端口不能为空",0,null);
		}else{
			String a = equipment.getHost();
			String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
			boolean matches = Pattern.compile(pattern).matcher(a).matches();
			if (matches==false) {
				return new Bo(false,"输入ip地址格式不正确",0,null);
			}
			int b = equipmentDao.insert(equipment);
			if (b==0) {
				return new Bo(false,"插入数据失败",0,null);
			}
			return new Bo(true,"",b,null);

		}
	}
	//改
	@Override
	public Bo update(Equipment equipment) {
		//参数校验
		if(equipment.getHost()==null || equipment.getHost().length()==0) {
			return new Bo(false,"设备ip地址为空",0,null);
		}else if (equipment.getPlcName()==null || equipment.getPlcName().length()==0) {
			return new Bo(false,"设备名称为空",0,null);
		}else if (equipment.getPort()==null) {
			return new Bo(false,"设备端口不能为空",0,null);
		}else{
			String a = equipment.getHost();
			String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
			boolean matches = Pattern.compile(pattern).matcher(a).matches();
			if (matches==false) {
				return new Bo(false,"输入ip地址格式不正确",0,null);
			}
			int b = equipmentDao.update(equipment);
			if (b==0) {
				return new Bo(false,"修改数据失败",0,null);
			}
			return new Bo(true,"",b,null);

		}
	}
	//删除
	@Override
	public Bo delete(Integer id) {
		if(id<=0) {
			return new Bo(false,"设备选择有误",0,null);
		}
		//根据设备id查询数据,判断这个通道有无数据
		int a = aisleDao.selectEquipmentId(id);
		if(a>0) {
			return new Bo(false,"设备下有测量通道,无法删除设备",0,null);
		}else {
			int b = equipmentDao.delete(id);
			if(b==0) {
				return new Bo(false,"删除设备数据失败",0,null);
			}
			return new Bo(true,"",b,null);
		}
	}
	//查询
	@Override
	public Bo select(Integer pages, Integer line) {
		//数据校验
		//页数
		if(pages==null || pages<0) {
			pages = 0;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		pages=pages*line;
		List<Equipment> list = equipmentDao.select(pages,line);
		return new Bo(true, "",list.size(),list);
	}
	//查询设备总数
	@Override
	public Bo selectSize() {
		int a = equipmentDao.selectSize();
		return new Bo(true, "",a,null);
	}
	@Override
	public Bo selectById(Integer id) {
		//数据校验
		if (id<=0) {
			return new Bo(false, "设备id错误",0,null);
		}else {
			Equipment equipment = equipmentDao.selectById(id);
			if (equipment==null) {
				return new Bo(false, "无此id数据",0,null);
			}else {
				return new Bo(true, "",10,equipment);
			}
		}
	}

}


